package com.patMall.controller;

import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.patMall.entity.Commodity;
import com.patMall.entity.OrderInfo;
import com.patMall.service.OrderInfoService;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.annotation.Resource;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @Author 750909637@qq.com
 * @PackageName patMall
 * @Package com.patMall.controller
 * @Date 2024/1/23 17:53
 * @Version 1.0
 */
@Controller
@RequestMapping("order")
public class OrderInfoController {
    @Resource
    private OrderInfoService orderInfoService;
    @PostMapping("addOrder")
    @ResponseBody
    public String addOrder(OrderInfo orderInfo) {
        orderInfo.setOrderId(System.currentTimeMillis());
        int count = orderInfoService.addOrder(orderInfo);
        if (count == 1) {
            return "success";
        }else {
            return "fail";
        }
    }
    @PostMapping("getOrderUsers")
    @ResponseBody
    public Object getOrderUsers(Integer userId) {
        Map map = new HashMap();
        List<OrderInfo> orderUsers = orderInfoService.getOrderUser(userId);
        if (orderUsers != null){
            map.put("msg","success");
            map.put("data",orderUsers);
            return map;
        }else {
            return map.put("msg","fail");
        }
    }
    @PostMapping("updateOrder")
    @ResponseBody
    public String updateOrder(OrderInfo orderInfo){
        orderInfo.setState(2);
        int count = orderInfoService.updateOrder(orderInfo);
        if (count == 1) {
            return "success";
        }else {
            return "fail";
        }
    }

    @PostMapping("getAllOrderInfo")
    @ResponseBody
    public Object getAllOrderInfo(int page,int limit){
        Map map = new HashMap();
        PageHelper.startPage(page,limit);
        List<OrderInfo> orderInfoList = orderInfoService.getOrderInfo();
        Page<OrderInfo> pageList = (Page<OrderInfo>) orderInfoList;
        long count = pageList.toPageInfo().getTotal();
        map.put("code",0);
        map.put("msg", "这是数据传来的！");
        map.put("count", count);
        map.put("data", pageList);
        return map;
    }
    @PostMapping("findOrderId")
    @ResponseBody
    public Object getOrderInfoId(int page,int limit,String orderId){
        Map map = new HashMap();
        PageHelper.startPage(page,limit);
        List<OrderInfo> orderInfoList = orderInfoService.findOrderInfoId(orderId);
        Page<OrderInfo> pageList = (Page<OrderInfo>) orderInfoList;
        long count = pageList.toPageInfo().getTotal();
        map.put("code",0);
        map.put("msg", "这是数据传来的！");
        map.put("count", count);
        map.put("data", pageList);
        return map;
    }
}
